package me.bincker.formwork.web.modules.system.entity

import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.userdetails.UserDetails

class SysUserDetails(user: User) : UserDetails{
    private val id: String = user.id.orEmpty()
    private val username: String = user.username.orEmpty()
    private val password: String = user.encodedPassword.orEmpty()
    private val authorities: List<SimpleGrantedAuthority> = user.roleList?.map { SimpleGrantedAuthority(it.code) } ?: emptyList()
    private val locked: Boolean = if(user.locked == null) false else user.locked!!

    override fun getUsername() = username

    override fun getAuthorities() = authorities

    override fun isEnabled() = true

    override fun isCredentialsNonExpired() = true

    override fun getPassword() = password

    override fun isAccountNonExpired() = true

    override fun isAccountNonLocked() = !locked

}